﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FusionDotNet
{
    /// <summary>
    /// Class for fluent validation of DataTable.
    /// </summary>
    internal static class TableValidation
    {
        internal static DataTable TableNotNull(this DataTable tableSchema)
        {
            string message = "The row provided is not attached to a data table.";
            return tableSchema.TableNotNull(message);
        }

        internal static DataTable TableNotNull(this DataTable tableSchema, string message)
        {
            if (tableSchema == null)
            {
                throw ExceptionBuilder.BuildException<InvalidOperationException>(message);
            }
            return tableSchema;
        }

        internal static DataTable TableHasName(this DataTable tableSchema)
        {
            if (String.IsNullOrEmpty(tableSchema.TableName))
            {
                string message = "The table provided does not have a name specified.";
                throw ExceptionBuilder.BuildException<InvalidOperationException>(message);
            }
            return tableSchema;
        }

        internal static DataTable TableHasColumns(this DataTable tableSchema)
        {
                if (tableSchema.Columns.Count == 0)
            {
                string message = "The row provided does not contain any columns.";
                throw ExceptionBuilder.BuildException<InvalidOperationException>(message);
            }
                return tableSchema;
        }

        internal static DataTable TableHasRowIdColumn(this DataTable tableSchema)
        {
            return tableSchema.TableHasColumn(Constants.FUSION_ROWID);
        }

        internal static DataTable TableHasColumn(this DataTable tableSchema, string columnName)
        {
            if (!tableSchema.Columns.Contains(columnName))
            {
                string message = String.Format("The DataTable does not contain a '{0}' column", columnName);
                throw ExceptionBuilder.BuildException<InvalidOperationException>(message);
            }
            return tableSchema;
        }
    }
}
